﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using BbsbWebService.Logging;
using System.IO;
using BbsbWebService.Classes.Controllers;

namespace BbsbWebService.Classes.Plugins.KeyMap
{
    public class KeyMapController : IController
    {
        #region IController Members

        public void SendCommand(string command)
        {
            // Get connection string from config.
            string serviceUrl = AppSettings.GetItemFromSettings<string>("KeyMapAddress", null);
            if (!serviceUrl.IsNullOrEmpty())
            {
                Uri requestUrl = null;
                try
                {
                    // Set up connection
                    if (!Uri.TryCreate(new Uri(serviceUrl), command, out requestUrl))
                        throw new ArgumentException("Unable to combine specified url values");

                    WebResponse objResponse;
                    WebRequest objRequest = HttpWebRequest.Create(requestUrl);

                    // Send command
                    objResponse = objRequest.GetResponse();
                }
                catch (Exception ex)
                {
                    Logging.Logger.Instance.LogException(ex, "Failed sending command '{0}', url: {1}", command, requestUrl);
                }
            }
            else
            {
                Logger.Instance.LogMessage("Could not find service url in the settings.");
            }
        }

        public void Listen()
        {
            // There is no listening, item status' can be updated via service call...
        }

        public void StopListening()
        {
            // There is no listening, so nothing to stop
        }

        #endregion
    }
}
